﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Examine;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Examine
{
    public class QcProjectServices : IQcProjectServices
    {
        public QcProjectServices(IBaseRepository db)
        {
            Db = db;
        }
        public IBaseRepository Db{ get; }
        public async Task<bool> AddAsync(QcProject model)
        {
            var result = await Db.InsertAsync(model);
            return result;
        }

        public  async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete QcProject where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete QcProject where Id = @Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }

        public async Task<IEnumerable<QcProject>> GetAllAsync()
        {
            string sql = "select * from QcProject";
            var list = await Db.QueryAllAsync<QcProject>(sql);
            return list;
        }

        public async Task<IEnumerable<QcProject>> GetAsync(int PageIndex, int PageSize, string ProjectCode, string ProjectName)
        {
            string sql = "select * from QcProject where 1=1 ";
            if (!string.IsNullOrEmpty(ProjectCode))
            {
                sql += " and ProjectCode like @ProjectCode";
            }

            if (!string.IsNullOrEmpty(ProjectName))
            {
                sql += " and ProjectName like @ProjectName";
            }
            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";

            var list = await Db.QueryAsync<QcProject>(sql, new { PageIndex, PageSize, ProjectCode = $"%{ProjectCode}%", ProjectName = $"%{ProjectName}%" }); 
            return list;

        }

        public async Task<int> GetCountAsync(string ProjectCode, string ProjectName)
        {
            string sql = "select count(*) from QcProject where 1=1";
            if (!string.IsNullOrEmpty(ProjectCode))
            {
                sql += " and ProjectCode like @ProjectCode";
            }

            if (!string.IsNullOrEmpty(ProjectName))
            {
                sql += " and ProjectName like @ProjectName";
            }
            var count = await Db.GetRecoreCountAsync(sql, new { ProjectCode = $"%{ProjectCode}%", ProjectName = $"%{ProjectName}%" });
            return count;
        }

        public async Task<bool> UpdateAsync(QcProject model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }
    }
}
